#include "EMW3080.h"

char  ATcmd[BUF_LEN];
char  ATbuffer[BUF_LEN];
char  ATdata[BUF_LEN_DATA];

bool WiFi_init(const char* _wifi_ssid, const char* _wifi_psw) {
  bool flag;

  flag = check_send_cmd(AT, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  flag = check_send_cmd(AT_REBOOT, AT_OK, 20);
  if (!flag)return false;
  delay(5000);

  flag = check_send_cmd(AT_ECHO_OFF, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  flag = check_send_cmd(AT_MSG_ON, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_WIFI_START, _wifi_ssid, _wifi_psw);
  flag = check_send_cmd(ATcmd, AT_WIFI_START_SUCC, 20);
  return flag;
}

bool Ali_connect(const char* _ProductKey, const char* _DeviceName, const char* _password) {
  bool flag;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_AUTH, _DeviceName, _ProductKey, _password);
  flag = check_send_cmd(ATcmd, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_CID, clientIDstr, timestamp);
  flag = check_send_cmd(ATcmd, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_SOCK, _ProductKey);
  flag = check_send_cmd(ATcmd, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  flag = check_send_cmd(AT_MQTT_AUTOSTART_OFF, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  flag = check_send_cmd(AT_MQTT_ALIVE, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  flag = check_send_cmd(AT_MQTT_START, AT_MQTT_START_SUCC, 20);
  if (!flag)return false;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_PUB_SET, _ProductKey, _DeviceName);
  flag = check_send_cmd(ATcmd, AT_OK, DEFAULT_TIMEOUT);
  if (!flag)return false;

  //flag = check_send_cmd(AT_MQTT_UNSUB,AT_OK,DEFAULT_TIMEOUT);
  //if(!flag)return false;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_SUB, _ProductKey, _DeviceName);
  flag = check_send_cmd(ATcmd, AT_MQTT_SUB_SUCC, DEFAULT_TIMEOUT);
//   if (!flag)BEEP(4);
  return flag;
}

bool Upload(const char* _pubTopic, const char* _JSON_DATA_PACK) {
  bool flag;
  int len;

  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_PUB_SET, _pubTopic);
  flag = check_send_cmd(ATcmd, AT_OK, DEFAULT_TIMEOUT);

  cleanBuffer(ATdata, BUF_LEN_DATA);

  /*JSON_DATA_PACK*//*intel开始是检测的数据*/
  len = snprintf(ATdata, BUF_LEN_DATA, JSON_DATA_PACK, _JSON_DATA_PACK);
  /*暂时理解为发送一个请求，等待返回">"后，发送json格式数据*/
  cleanBuffer(ATcmd, BUF_LEN);
  snprintf(ATcmd, BUF_LEN, AT_MQTT_PUB_DATA, len - 1);
  flag = check_send_cmd(ATcmd, ">", DEFAULT_TIMEOUT);
  if (flag) flag = check_send_cmd(ATdata, AT_MQTT_PUB_DATA_SUCC, 20);
}

bool check_send_cmd(const char* cmd, const char* resp, unsigned int timeout) {
  int i = 0;
  unsigned long timeStart;
  timeStart = millis();
  cleanBuffer(ATbuffer, BUF_LEN);
  Serial.print(cmd);
  Serial.flush();
  while (1) {
    while (Serial.available()) {
      ATbuffer[i++] = Serial.read();
      if (i >= BUF_LEN)i = 0;
    }
    if (NULL != strstr(ATbuffer, resp))break;
    if ((unsigned long)(millis() - timeStart > timeout * 1000)) break;
  }

  if (NULL != strstr(ATbuffer, resp))return true;
  return false;
}

void cleanBuffer(char *buf, int len) {
  for (int i = 0; i < len; i++)  {
    buf[i] = '\0';
  }
}